package com.ibm.risk.irmp.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

@Slf4j
public class BeanMapUtils {

    public static final String CLASS_NAME_KEY = "className";

    @Setter
    private static ObjectMapper om;

    static ObjectMapper getObjectMapper() {
        if (om == null) {
            om = new ObjectMapper();
            om.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
        return om;
    }

    /**
     * 把bean的属性浅复制到map
     *
     * @param source
     * @param target
     */
    public static void copyToMap(Object source, Map<String, Object> target) {
        try {
            PropertyUtils.describe(source).forEach((k, v1) -> {
                if (!"class".equals(k) && v1 != null) {
                    target.put((String) k, v1);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 把map浅复制到bean
     *
     * @param source
     * @param target
     */
    public static void copyToBean(Map<String, Object> source, Object target) {
        try {
            Set<Map.Entry> set = PropertyUtils.describe(target).entrySet();
            for (Map.Entry prop : set) {
                String k = (String) prop.getKey();
                Object v = prop.getValue();
                if (!"class".equals(k) && source.containsKey(k)) {
                    PropertyUtils.setProperty(target, k, source.get(k));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, Object> beanToMap(Object bean) {
        if (bean instanceof Object[] || bean instanceof Collection) {
            throw new RuntimeException("本方法不支持集合或数组");
        }
        try {
            String s = getObjectMapper().writeValueAsString(bean);
            Map map = getObjectMapper().readValue(s, Map.class);
            String name = bean.getClass().getName();
            map.put(CLASS_NAME_KEY, name);
            log.trace("class: {}", name);
            return map;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T mapToBean(Map<?, ?> map, Class<T> clz) {
        try {
            String s = getObjectMapper().writeValueAsString(map);
            return getObjectMapper().readValue(s, clz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Object mapToBean(Map<?, ?> map) {
        try {
            Class<?> clz = Class.forName((String) map.get(CLASS_NAME_KEY));
            String s = getObjectMapper().writeValueAsString(map);
            return getObjectMapper().readValue(s, clz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
